package org.example;

import com.alibaba.fastjson.JSON;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.openai.OpenAiChatModel;

/**
 * @author fish_temp_author
 * @since fish_temp_since
 */
public class ChatMemoryDemo {


    public static void main(String[] args) {
        OpenAiChatModel openAiChatModel = OpenAiChatModel.builder()
                .apiKey("demo")
                .modelName("gpt-4o-mini")
                .build();

        ChatMemory chatMemory = MessageWindowChatMemory
                .withMaxMessages(10);

        chatMemory.add(UserMessage.from("你好，请记住你的名字叫小明，你最好的朋友是小星星"));

        ChatResponse chat = openAiChatModel.chat(chatMemory.messages());
        AiMessage aiMessage = chat.aiMessage();
        chatMemory.add(aiMessage);
        System.out.println(JSON.toJSONString(aiMessage.text()));

        chatMemory.add(UserMessage.from("你的名字叫什么?"));
        AiMessage aiMessage1 = openAiChatModel.chat(chatMemory.messages()).aiMessage();
        System.out.println(JSON.toJSONString(aiMessage1.text()));

        chatMemory.add(UserMessage.from("你最好的朋友是谁？"));
        AiMessage aiMessage2 = openAiChatModel.chat(chatMemory.messages()).aiMessage();
        System.out.println(JSON.toJSONString(aiMessage2.text()));

    }
}
